/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RelatorioDePedidosDialog.java
 *
 * Created on Nov 12, 2010, 1:55:20 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleRelatorio;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.utils.DiasUteis;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego Freire
 */
public class RelatorioContaCorrenteDialog extends javax.swing.JDialog {

    public RelatorioContaCorrenteDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setResizable(false);
        setLocationRelativeTo(null);
        data1TextField.setDate(new Date());
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        separador1 = new javax.swing.JSeparator();
        data1 = new javax.swing.JLabel();
        data1TextField = new org.jdesktop.swingx.JXDatePicker();
        data2 = new javax.swing.JLabel();
        data2TextField = new org.jdesktop.swingx.JXDatePicker();
        separador2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        metaDiariaField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        diasUteisField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        limparJButton = new javax.swing.JButton();
        emitirButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("TBP - Relatório Conta Corrente");

        separador1.setForeground(new java.awt.Color(153, 153, 153));

        data1.setText("Data Inicial:");

        data2.setText("Data Final:");

        separador2.setForeground(new java.awt.Color(153, 153, 153));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/venda32.png"))); // NOI18N
        jLabel1.setText("Selecione a data inicial e final, então clique para emitir o relatório de conta corrente.");

        jLabel2.setText("Meta Diária:");

        metaDiariaField.setFont(new java.awt.Font("Dialog", 1, 12));
        metaDiariaField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        metaDiariaField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                metaDiariaFieldFocusGained(evt);
            }
        });

        jLabel3.setText("Dias Úteis:");

        diasUteisField.setEditable(false);
        diasUteisField.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        diasUteisField.setForeground(new java.awt.Color(51, 51, 255));
        diasUteisField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        diasUteisField.setFocusable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(separador1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                            .addComponent(jLabel1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(data2)
                            .addComponent(data1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(diasUteisField)
                            .addComponent(data2TextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(data1TextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(metaDiariaField)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(separador2, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separador1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(data1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(data1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(data2)
                    .addComponent(data2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(diasUteisField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(metaDiariaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(separador2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        limparJButton.setMnemonic('l');
        limparJButton.setText("Limpar");
        limparJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparJButtonActionPerformed(evt);
            }
        });
        jPanel2.add(limparJButton);

        emitirButton.setMnemonic('e');
        emitirButton.setText("Emitir");
        emitirButton.setPreferredSize(new java.awt.Dimension(100, 26));
        emitirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emitirButtonActionPerformed(evt);
            }
        });
        jPanel2.add(emitirButton);

        fecharButton.setMnemonic('f');
        fecharButton.setText("Fechar");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });
        jPanel2.add(fecharButton);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limparJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparJButtonActionPerformed
        limparCampos();
}//GEN-LAST:event_limparJButtonActionPerformed

    private void emitirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emitirButtonActionPerformed
        Date inicio = data1TextField.getDate();
        Date fim = data2TextField.getDate();
        if (fim == null) {
            fim = inicio;
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(inicio);
        cal2.setTime(fim);
        if (cal1.compareTo(cal2) == 1) {
            JOptionPane.showMessageDialog(null, "A data inicial não pode ser posterior a data final.", "Mensagem do sistema", 2);
            data1TextField.requestFocus();
            return;
        }
        try {
            int diasUteis = DiasUteis.getWorkingDays(inicio, fim);
            int metaDiaria = Integer.parseInt(metaDiariaField.getText());
            int acessos = diasUteis * metaDiaria;
            System.out.println(acessos);
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(null, "A meta diária não foi informada corretamente.", "Mensagem do sistema", 2);
            metaDiariaField.requestFocus();
        }
}//GEN-LAST:event_emitirButtonActionPerformed

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
}//GEN-LAST:event_fecharButtonActionPerformed

    private void metaDiariaFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_metaDiariaFieldFocusGained
        Date inicio = data1TextField.getDate();
        Date fim = data2TextField.getDate();
        if (fim == null) {
            fim = inicio;
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(inicio);
        cal2.setTime(fim);
        if (cal1.compareTo(cal2) == 1) {
            return;
        }
        try {
            int diasUteis = DiasUteis.getWorkingDays(inicio, fim);
            diasUteisField.setText(String.valueOf(diasUteis));
        } catch (NumberFormatException nfe) {
            return;
        }
    }//GEN-LAST:event_metaDiariaFieldFocusGained
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel data1;
    private org.jdesktop.swingx.JXDatePicker data1TextField;
    private javax.swing.JLabel data2;
    private org.jdesktop.swingx.JXDatePicker data2TextField;
    private javax.swing.JTextField diasUteisField;
    private javax.swing.JButton emitirButton;
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton limparJButton;
    private javax.swing.JTextField metaDiariaField;
    private javax.swing.JSeparator separador1;
    private javax.swing.JSeparator separador2;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private void limparCampos() {
        data1TextField.setDate(null);
        data2TextField.setDate(null);
        data1TextField.requestFocus();
        metaDiariaField.setText(null);
    }

    public static void main(String[] args) {
        new RelatorioContaCorrenteDialog(null, true).setVisible(true);
    }
}
